package org.scboy.dbms.valid.common;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.scboy.dbms.annotation.ValidType;
import org.scboy.dbms.entity.ParamConfig;
import org.scboy.dbms.valid.Valid;
import org.scboy.utils.entitys.coobi.Model;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;

/**
 * 正则表达式验证
 * @author yanghao
 *
 */
@ValidType("正则校验")
public class RegxValid implements Valid  {

	@Override
	public List<ParamConfig> config() {
		List<ParamConfig> ParamConfigs=new ArrayList<ParamConfig>();
		ParamConfigs.add(new ParamConfig("expression", "表达式","正则表达式。"));
		ParamConfigs.add(new ParamConfig("msg", "消息文字","提示消息的文字。"));
		return ParamConfigs;
	}

	@Override
	public String toString(Model config) {
		return config.getString("msg");
	}

	@Override
	public boolean valid(Object value,Model model, Model config) {
		String data=Convert.toStr(value);
		if(StrUtil.isNotEmpty(data)){
			String exp=config.getString("expression");
			Pattern pattern=Pattern.compile(exp);
			Matcher matcher=pattern.matcher(data);
			if(matcher.matches()){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	}

}
